
#include <stdint.h>
#include "ad5620.h"
#include "os.h"

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  simulation_spi_write
 *  Description:  模拟SPI接口的写入操作
 * =====================================================================================
 */
void simulation_spi_write(AD_Chip_Para *ptr, uint16_t data){

	uint16_t i;

	ptr->CK_LO();
	ptr->CS_LO();

	for ( i = 0; i < 16; i += 1 ) {
		if((data<<i)&0x8000){
			ptr->DO_HI();
		}else{
			ptr->DO_LO();
		}  
		ptr->CK_HI();
		ptr->CK_LO();
	}
//	ptr->CK_HI();
	ptr->CS_HI();

	return ;
}		/* -----  end of function simulation_spi_write  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  simulation_spi_write_byte
 *  Description:  
 * =====================================================================================
 */
void simulation_spi_write_byte(AD_Chip_Para *ptr, uint8_t data){

	uint16_t i;

	ptr->CK_LO();
	ptr->CS_LO();
	for ( i = 0; i < 8; i += 1 ) {
		if((data<<i)&0x80){
			ptr->DO_HI();
		}else{
			ptr->DO_LO();
		}  
		ptr->CK_HI();
		ptr->CK_LO();
	}
	ptr->CK_HI();
	ptr->CS_HI();

	return ;
}		/* -----  end of function simulation_spi_write_byte  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  ad5620_data_send
 *  Description:  
 * =====================================================================================
 */
void ad5620_data_send(AD_Chip_Para *ptr, uint16_t data){

	simulation_spi_write(ptr, ((data << 2) & 0x3ffc));
        
	return ;
}		/* -----  end of function ad5620_data_send  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  mcp4161_data_send
 *  Description:  发送数据到MCP4161
 * =====================================================================================
 */
void mcp4161_data_send(AD_Chip_Para *ptr, uint16_t data){

    uint16_t send_data = 0;
    OS_ERR os_err;

    send_data = 0;
    send_data |= (data & 0x03FF); //添加要写入的数据
    simulation_spi_write(ptr, send_data);

    OSTimeDly(10, OS_OPT_TIME_DLY, &os_err);
    send_data = 0;
    send_data |= (data & 0x03FF); //添加要写入的数据
     send_data |= 0x2000; //添加非易失性抽头0的地址到高4位
    simulation_spi_write(ptr, send_data);

//    OSTimeDly(10, OS_OPT_TIME_DLY, &os_err);
//    send_data |= (data & 0x03FF); //添加要写入的数据
//    send_data |= 0x3000; //添加非易失性抽头0的地址到高4位
//    simulation_spi_write(ptr, send_data);

	return ;
}		/* -----  end of function mcp4161_data_send  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  mcp4161_wp_disable
 *  Description:  
 * =====================================================================================
 */
void mcp4161_wp_disable(AD_Chip_Para *ptr) {

//    simulation_spi_write_byte(ptr, 0xF4);
//    simulation_spi_write_byte(ptr, 0x24);

    return;
} /* -----  end of function mcp4161_wp_disable  ----- */

